/*
 * Copyright 2008 Zoundry LLC, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.zoundry.gaedt.core.models;

import java.lang.reflect.Method;

import javax.xml.namespace.QName;

import org.apache.commons.lang.WordUtils;

/**
 * A base implementation of a mutator provider.  This implementation will try 
 * to find the mutator by looking for typical POJO/Bean method with the right
 * name and type.
 */
public class ZGaeModelMutatorProvider implements IZGaeModelMutatorProvider
{
    /**
     * @see org.zoundry.gaedt.core.models.IZGaeModelMutatorProvider#getMutatorForAttribute(org.zoundry.gaedt.core.models.ZGaeModel, javax.xml.namespace.QName, java.lang.Object)
     */
    public Method getMutatorForAttribute(ZGaeModel aModel, QName aName, Object aValue)
    {
        if (aName.getNamespaceURI() != null && !"".equals(aName.getNamespaceURI()))
            throw new RuntimeException("This mutator provider does not support qualified attribute.");
        String mutatorName = "set" + WordUtils.capitalize(aName.getLocalPart());
        try
        {
            return aModel.getClass().getMethod(mutatorName, aValue.getClass());
        }
        catch (Exception e)
        {
            // Could not find the method at the standard location.
        }
        return null;
    }
    
    /**
     * @see org.zoundry.gaedt.core.models.IZGaeModelMutatorProvider#getMutatorForChild(org.zoundry.gaedt.core.models.ZGaeModel, javax.xml.namespace.QName, org.zoundry.gaedt.core.models.ZGaeModel)
     */
    public Method getMutatorForChild(ZGaeModel aModel, QName aElementName, ZGaeModel aParentModel)
    {
        String mutatorName = "set" + WordUtils.capitalize(aElementName.getLocalPart());
        try
        {
            return aParentModel.getClass().getMethod(mutatorName, aModel.getClass());
        }
        catch (Exception e)
        {
        }
        mutatorName = "add" + WordUtils.capitalize(aElementName.getLocalPart());
        try
        {
            return aParentModel.getClass().getMethod(mutatorName, aModel.getClass());
        }
        catch (Exception e)
        {
        }
        return null;
    }

    /**
     * @see org.zoundry.gaedt.core.models.IZGaeModelMutatorProvider#getMutatorForElementValue(org.zoundry.gaedt.core.models.ZGaeModel, java.lang.Object)
     */
    public Method getMutatorForElementValue(ZGaeModel aModel, Object aModelValue)
    {
        String mutatorName = "setValue";
        try
        {
            return aModel.getClass().getMethod(mutatorName, aModelValue.getClass());
        }
        catch (Exception e)
        {
        }
        return null;
    }
}
